//
//  UIView+Support.m
//  AMS
//
//  Created by l.t.zero on 2020/6/5.
//  Copyright © 2020 l.t.zero. All rights reserved.
//

#import "UIView+Support.h"

@implementation UINavigationBar(line)

- (void)hideline{
    
    [self findSubViewByCompare:^BOOL(UIView *view) {
        if ([view isKindOfClass:[UIImageView class]]&& CGRectGetHeight(view.bounds) <= 1){
            view.hidden = YES;
        }
        return YES;
    } allSameType:YES container:nil];
}

@end

@implementation UIView(SupportEx)

/**
 *  将当前view渲染成image对象
 */
- (UIImage *)renderImage{
    return [self renderImageWithSize:self.bounds.size];
}

/**
 *  将当前view渲染成指定大小的image对象
 */
- (UIImage *)renderImageWithSize:(CGSize)size{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
/**
 *  从xib加载view通用方法
 */
+ (instancetype)viewFromXib{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}

- (UIViewController *)viewController{
    UIResponder *next = self.nextResponder;
    
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        
        next = next.nextResponder;
    } while (next);
    
    return nil;
}

- (UINavigationController *)navigationController{
    UIResponder *next = self.nextResponder;
    
    do {
        if ([next isKindOfClass:[UINavigationController class]]) {
            return (UINavigationController *)next;
        }
        
        next = next.nextResponder;
    } while (next);
    
    return nil;
}
+ (BOOL)findSubViewInView:(UIView*)v compare:(BOOL (^)(UIView *obj))comparision allSameType:(BOOL)same container:(NSArray * __autoreleasing *)container{
    @try {

        if (container && !*container){
            *container = [NSMutableArray new];
        }
        return [self findSubViewInViewInner:v compare:comparision allSameType:same container:(NSMutableArray *)(container != NULL?*container:nil)];
    } @catch (NSException *exception) {
        return NO;
    }
}
+ (BOOL)findSubViewInViewInner:(UIView*)v compare:(BOOL (^)(UIView *obj))comparision allSameType:(BOOL)same container:(NSMutableArray *)container{
    __block BOOL found = NO;
    [v.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (comparision && comparision(obj)) {
            found = YES;
            *stop = !same;
            [container addObject:obj];
        }
    }];
    
    if (same || container.count <= 0) {
        [v.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            BOOL tmp = [self findSubViewInViewInner:obj compare:comparision allSameType:same container:container];
            found = found?YES:tmp;
            *stop = found && !same;
            
        }];
    }
    
    return found;
}
- (BOOL)findSubViewByCompare:(BOOL (^)(UIView *obj))comparision allSameType:(BOOL)same container:(NSArray * __autoreleasing *)container{
    return [[self class] findSubViewInView:self compare:comparision allSameType:same container:container];
}

- (UIGestureRecognizer *)tap:(id)observer selector:(SEL)sel{
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:observer action:sel];
    [self addGestureRecognizer:gesture];
    
    self.userInteractionEnabled = YES;
    
    return gesture;
}

@end
